package com.situ.springboot.job;

import com.situ.springboot.constant.RedisConstants;
import com.situ.springboot.util.AliOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.ObjectUtils;

import java.util.Set;

@Configuration
public class DeleteImageJob {

    @Autowired
    RedisTemplate redisTemplate;

    @Scheduled(cron = "0 0/2 * * * ?")
    public void deleteImage() {
        System.out.println("DeleteImageJob.deleteImage");
        Set<String> set = redisTemplate.opsForSet().difference(RedisConstants.UPLOAD_IMAGE, RedisConstants.UPLOAD_IMAGE_TO_DB);
        if (!ObjectUtils.isEmpty(set)) {
            for (String image : set) {
                AliOSSUtil.deleteFile(image);
            }
            redisTemplate.delete(RedisConstants.UPLOAD_IMAGE);
            redisTemplate.delete(RedisConstants.UPLOAD_IMAGE_TO_DB);
        }
    }
}
